package com.ruoyi.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.websocket.WebSocketServer;
import com.ruoyi.domain.JkPush;
import com.ruoyi.service.IJkPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 推送管理Controller
 *
 * @author ruoyi
 * @date 2023-09-12
 */
@EnableScheduling
@RestController
@RequestMapping("/push/push")
public class JkPushController extends BaseController
{
    @Autowired
    private IJkPushService jkPushService;

    @Autowired
    private WebSocketServer webSocketServer;

    /**
     * 查询推送管理列表
     */
    @PreAuthorize("@ss.hasPermi('push:push:list')")
    @GetMapping("/list")
    public TableDataInfo list(JkPush jkPush)
    {
        startPage();
        List<JkPush> list = jkPushService.selectJkPushList(jkPush);
        return getDataTable(list);
    }

    /**
     * 导出推送管理列表
     */
    @PreAuthorize("@ss.hasPermi('push:push:export')")
    @Log(title = "推送管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JkPush jkPush)
    {
        List<JkPush> list = jkPushService.selectJkPushList(jkPush);
        ExcelUtil<JkPush> util = new ExcelUtil<JkPush>(JkPush.class);
        util.exportExcel(response, list, "推送管理数据");
    }

    /**
     * 获取推送管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('push:push:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(jkPushService.selectJkPushById(id));
    }

    /**
     * 新增推送管理
     */
    @PreAuthorize("@ss.hasPermi('push:push:add')")
    @Log(title = "推送管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JkPush jkPush)
    {
        return toAjax(jkPushService.insertJkPush(jkPush));
    }

    /**
     * 修改推送管理
     */
    @PreAuthorize("@ss.hasPermi('push:push:edit')")
    @Log(title = "推送管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JkPush jkPush)
    {
        return toAjax(jkPushService.updateJkPush(jkPush));
    }

    /**
     * 删除推送管理
     */
    @PreAuthorize("@ss.hasPermi('push:push:remove')")
    @Log(title = "推送管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jkPushService.deleteJkPushByIds(ids));
    }

//    @Scheduled(cron = "0 0 8 * * ?")
    @GetMapping("/pushMessage")
    public void startJob() throws IOException {
        jkPushService.startJob();
    }

    @Scheduled(cron = "0 0 8 * * ?")
    @GetMapping("/pushMessageByGroupUser")
    public void pushMessageByGroupUser() throws IOException {
        jkPushService.pushMessageByGroupUser();
    }

    @GetMapping("/sendMessage")
    public void sendMessage(String pageId) throws IOException {
        System.out.println("pageId:"+pageId);
        webSocketServer.sendMessageTo("refresh",pageId);
    }
}
